package com.aqie.easy.bitOperation;

/**
 * 693 交替位二进制数
 */
public class HasAlternatingBits {
    public boolean hasAlternatingBits(int n) {
        // int 转 二进制
        String s = Integer.toBinaryString(n);
        for (int i = 1; i < s.length(); i++){
            if (s.charAt(i) == s.charAt(i - 1)){
                return false;
            }
        }
        return true;
    }

    public boolean hasAlternatingBits2(int n) {
        n = (n ^ (n >> 1));
        return (n & ((long)n + 1)) == 0;
    }
}
